{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "4000条训练数据机器学习20210224ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sunyingjian/AI-in-well-logging/blob/master/%3CConcise%20code%3ESVM%20Random%20Forests%20GBDT%20XGBoost%20facies%20classification.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dm93S7q9R6hv"
      },
      "source": [
        "# 导入数据"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7e3uu9LGVC3C",
        "outputId": "32298d78-1595-4043-94e9-8666dddb475e"
      },
      "source": [
        "!git clone https://github.com/sunyingjian/tutorials-2016.git"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "fatal: destination path 'tutorials-2016' already exists and is not an empty directory.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "L1CbkKBselzz",
        "outputId": "79d93f84-8eca-4f58-8f79-5c5191569bb7"
      },
      "source": [
        "!git clone https://github.com/sunyingjian/numpy-.git"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'numpy-'...\n",
            "remote: Enumerating objects: 15, done.\u001b[K\n",
            "remote: Counting objects: 100% (15/15), done.\u001b[K\n",
            "remote: Compressing objects: 100% (15/15), done.\u001b[K\n",
            "remote: Total 418 (delta 4), reused 0 (delta 0), pack-reused 403\u001b[K\n",
            "Receiving objects: 100% (418/418), 202.27 MiB | 32.19 MiB/s, done.\n",
            "Resolving deltas: 100% (118/118), done.\n",
            "Checking out files: 100% (206/206), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0kERhLWAdA4N",
        "outputId": "fe9174c8-e693-4eab-bb83-e33fc197a958"
      },
      "source": [
        "! /opt/bin/nvidia-smi"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Thu Feb 25 06:48:47 2021       \n",
            "+-----------------------------------------------------------------------------+\n",
            "| NVIDIA-SMI 460.32.03    Driver Version: 460.32.03    CUDA Version: 11.2     |\n",
            "|-------------------------------+----------------------+----------------------+\n",
            "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
            "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
            "|                               |                      |               MIG M. |\n",
            "|===============================+======================+======================|\n",
            "|   0  Tesla T4            Off  | 00000000:00:04.0 Off |                    0 |\n",
            "| N/A   33C    P8     9W /  70W |      0MiB / 15109MiB |      0%      Default |\n",
            "|                               |                      |                  N/A |\n",
            "+-------------------------------+----------------------+----------------------+\n",
            "                                                                               \n",
            "+-----------------------------------------------------------------------------+\n",
            "| Processes:                                                                  |\n",
            "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
            "|        ID   ID                                                   Usage      |\n",
            "|=============================================================================|\n",
            "|  No running processes found                                                 |\n",
            "+-----------------------------------------------------------------------------+\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kNuEGLZZR6hv"
      },
      "source": [
        "import tensorflow as tf\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "from matplotlib import pyplot as plt\n",
        "from tensorflow import keras\n",
        "%matplotlib inline"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GKjMViqMR6hw"
      },
      "source": [
        "training_data = pd.read_csv('/content/numpy-/美国分井/train_data_处理完成 (1).csv')\n",
        "blind_data = pd.read_csv('/content/numpy-/美国分井/blind_data_处理完成.csv')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9aCB_eP1Ue66"
      },
      "source": [
        "blind = pd.read_csv('/content/numpy-/美国油田/blind_data_wellname.csv')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        },
        "id": "oUFDfziXNXLj",
        "outputId": "6f279087-162d-4d8b-d6f7-11e0655253b5"
      },
      "source": [
        "training_data"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Unnamed: 0</th>\n",
              "      <th>Formation</th>\n",
              "      <th>Depth</th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "      <th>Facies</th>\n",
              "      <th>Well Name</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.399818</td>\n",
              "      <td>0.298966</td>\n",
              "      <td>0.458149</td>\n",
              "      <td>0.776042</td>\n",
              "      <td>0.219321</td>\n",
              "      <td>0.557385</td>\n",
              "      <td>0.0</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.400729</td>\n",
              "      <td>0.302738</td>\n",
              "      <td>0.456157</td>\n",
              "      <td>0.888021</td>\n",
              "      <td>0.231865</td>\n",
              "      <td>0.494046</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.978788</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.401639</td>\n",
              "      <td>0.306417</td>\n",
              "      <td>0.454165</td>\n",
              "      <td>0.903646</td>\n",
              "      <td>0.241224</td>\n",
              "      <td>0.430707</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.956566</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>3</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.402550</td>\n",
              "      <td>0.339247</td>\n",
              "      <td>0.452173</td>\n",
              "      <td>0.880208</td>\n",
              "      <td>0.242479</td>\n",
              "      <td>0.418039</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.935354</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>4</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.403461</td>\n",
              "      <td>0.285601</td>\n",
              "      <td>0.446860</td>\n",
              "      <td>0.869792</td>\n",
              "      <td>0.246049</td>\n",
              "      <td>0.405371</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.914141</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3227</th>\n",
              "      <td>3227</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.996357</td>\n",
              "      <td>0.155858</td>\n",
              "      <td>0.646070</td>\n",
              "      <td>0.565833</td>\n",
              "      <td>0.129373</td>\n",
              "      <td>0.432860</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.681818</td>\n",
              "      <td>5</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3228</th>\n",
              "      <td>3228</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.997268</td>\n",
              "      <td>0.145818</td>\n",
              "      <td>0.650055</td>\n",
              "      <td>0.576589</td>\n",
              "      <td>0.144021</td>\n",
              "      <td>0.398277</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.673737</td>\n",
              "      <td>5</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3229</th>\n",
              "      <td>3229</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.998179</td>\n",
              "      <td>0.169829</td>\n",
              "      <td>0.657359</td>\n",
              "      <td>0.594401</td>\n",
              "      <td>0.144021</td>\n",
              "      <td>0.378769</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.665657</td>\n",
              "      <td>5</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3230</th>\n",
              "      <td>3230</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.999089</td>\n",
              "      <td>0.177978</td>\n",
              "      <td>0.658023</td>\n",
              "      <td>0.598516</td>\n",
              "      <td>0.138135</td>\n",
              "      <td>0.373955</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.657576</td>\n",
              "      <td>5</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3231</th>\n",
              "      <td>3231</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.171282</td>\n",
              "      <td>0.661343</td>\n",
              "      <td>0.586172</td>\n",
              "      <td>0.118065</td>\n",
              "      <td>0.392070</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.649495</td>\n",
              "      <td>5</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>3232 rows × 12 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "      Unnamed: 0  Formation     Depth  ...    RELPOS  Facies  Well Name\n",
              "0              0   0.076923  0.399818  ...  1.000000       3          7\n",
              "1              1   0.076923  0.400729  ...  0.978788       3          7\n",
              "2              2   0.076923  0.401639  ...  0.956566       3          7\n",
              "3              3   0.076923  0.402550  ...  0.935354       3          7\n",
              "4              4   0.076923  0.403461  ...  0.914141       3          7\n",
              "...          ...        ...       ...  ...       ...     ...        ...\n",
              "3227        3227   0.923077  0.996357  ...  0.681818       5          0\n",
              "3228        3228   0.923077  0.997268  ...  0.673737       5          0\n",
              "3229        3229   0.923077  0.998179  ...  0.665657       5          0\n",
              "3230        3230   0.923077  0.999089  ...  0.657576       5          0\n",
              "3231        3231   0.923077  1.000000  ...  0.649495       5          0\n",
              "\n",
              "[3232 rows x 12 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BELUGB7HNhHz"
      },
      "source": [
        "training_data1 = pd.read_csv('/content/numpy-/block_TCN_处理完成.csv')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        },
        "id": "L-6VEKZtNnk4",
        "outputId": "3482e0f6-2423-433d-a7b6-3f7b2e8736c3"
      },
      "source": [
        "training_data1"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Formation</th>\n",
              "      <th>Depth</th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "      <th>Facies</th>\n",
              "      <th>Well Name</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.575189</td>\n",
              "      <td>0.289898</td>\n",
              "      <td>0.464680</td>\n",
              "      <td>0.348468</td>\n",
              "      <td>0.385598</td>\n",
              "      <td>0</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>3</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.001996</td>\n",
              "      <td>0.604475</td>\n",
              "      <td>0.272564</td>\n",
              "      <td>0.647994</td>\n",
              "      <td>0.433656</td>\n",
              "      <td>0.373814</td>\n",
              "      <td>0</td>\n",
              "      <td>0.973764</td>\n",
              "      <td>3</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.003992</td>\n",
              "      <td>0.617873</td>\n",
              "      <td>0.270173</td>\n",
              "      <td>0.690625</td>\n",
              "      <td>0.473574</td>\n",
              "      <td>0.347765</td>\n",
              "      <td>0</td>\n",
              "      <td>0.948537</td>\n",
              "      <td>3</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.005988</td>\n",
              "      <td>0.623511</td>\n",
              "      <td>0.270173</td>\n",
              "      <td>0.669310</td>\n",
              "      <td>0.488738</td>\n",
              "      <td>0.344505</td>\n",
              "      <td>0</td>\n",
              "      <td>0.922301</td>\n",
              "      <td>3</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.007984</td>\n",
              "      <td>0.612382</td>\n",
              "      <td>0.265989</td>\n",
              "      <td>0.618152</td>\n",
              "      <td>0.473351</td>\n",
              "      <td>0.343765</td>\n",
              "      <td>0</td>\n",
              "      <td>0.896065</td>\n",
              "      <td>3</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>900</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.992016</td>\n",
              "      <td>0.555516</td>\n",
              "      <td>0.335326</td>\n",
              "      <td>0.645820</td>\n",
              "      <td>0.398800</td>\n",
              "      <td>0.380910</td>\n",
              "      <td>1</td>\n",
              "      <td>0.399596</td>\n",
              "      <td>4</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>901</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.994012</td>\n",
              "      <td>0.665442</td>\n",
              "      <td>0.361626</td>\n",
              "      <td>0.612994</td>\n",
              "      <td>0.411066</td>\n",
              "      <td>0.395389</td>\n",
              "      <td>1</td>\n",
              "      <td>0.387487</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>902</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.996008</td>\n",
              "      <td>0.343731</td>\n",
              "      <td>0.371189</td>\n",
              "      <td>0.528542</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.408255</td>\n",
              "      <td>1</td>\n",
              "      <td>0.375378</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>903</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.998004</td>\n",
              "      <td>0.980854</td>\n",
              "      <td>0.325762</td>\n",
              "      <td>0.570746</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.402142</td>\n",
              "      <td>1</td>\n",
              "      <td>0.363269</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>904</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.536747</td>\n",
              "      <td>0.355051</td>\n",
              "      <td>0.590101</td>\n",
              "      <td>0.496633</td>\n",
              "      <td>0.401388</td>\n",
              "      <td>1</td>\n",
              "      <td>0.351160</td>\n",
              "      <td>4</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>905 rows × 11 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "     Formation     Depth        GR  ...    RELPOS  Facies  Well Name\n",
              "0     0.076923  0.000000  0.575189  ...  1.000000       3          8\n",
              "1     0.076923  0.001996  0.604475  ...  0.973764       3          8\n",
              "2     0.076923  0.003992  0.617873  ...  0.948537       3          8\n",
              "3     0.076923  0.005988  0.623511  ...  0.922301       3          8\n",
              "4     0.076923  0.007984  0.612382  ...  0.896065       3          8\n",
              "..         ...       ...       ...  ...       ...     ...        ...\n",
              "900   0.923077  0.992016  0.555516  ...  0.399596       4          9\n",
              "901   0.923077  0.994012  0.665442  ...  0.387487       6          9\n",
              "902   0.923077  0.996008  0.343731  ...  0.375378       6          9\n",
              "903   0.923077  0.998004  0.980854  ...  0.363269       6          9\n",
              "904   0.923077  1.000000  0.536747  ...  0.351160       4          9\n",
              "\n",
              "[905 rows x 11 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5UvQsSCbNqzr"
      },
      "source": [
        "training_data = training_data.drop(columns='Unnamed: 0')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wKVIpmeDN0yd"
      },
      "source": [
        "training_data = pd.concat([training_data,training_data1])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        },
        "id": "Z0yjtk8OOOoF",
        "outputId": "902c5ac4-740b-4196-d73d-d5d261540310"
      },
      "source": [
        "training_data"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Formation</th>\n",
              "      <th>Depth</th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "      <th>Facies</th>\n",
              "      <th>Well Name</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.399818</td>\n",
              "      <td>0.298966</td>\n",
              "      <td>0.458149</td>\n",
              "      <td>0.776042</td>\n",
              "      <td>0.219321</td>\n",
              "      <td>0.557385</td>\n",
              "      <td>0.0</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.400729</td>\n",
              "      <td>0.302738</td>\n",
              "      <td>0.456157</td>\n",
              "      <td>0.888021</td>\n",
              "      <td>0.231865</td>\n",
              "      <td>0.494046</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.978788</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.401639</td>\n",
              "      <td>0.306417</td>\n",
              "      <td>0.454165</td>\n",
              "      <td>0.903646</td>\n",
              "      <td>0.241224</td>\n",
              "      <td>0.430707</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.956566</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.402550</td>\n",
              "      <td>0.339247</td>\n",
              "      <td>0.452173</td>\n",
              "      <td>0.880208</td>\n",
              "      <td>0.242479</td>\n",
              "      <td>0.418039</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.935354</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.403461</td>\n",
              "      <td>0.285601</td>\n",
              "      <td>0.446860</td>\n",
              "      <td>0.869792</td>\n",
              "      <td>0.246049</td>\n",
              "      <td>0.405371</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.914141</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>900</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.992016</td>\n",
              "      <td>0.555516</td>\n",
              "      <td>0.335326</td>\n",
              "      <td>0.645820</td>\n",
              "      <td>0.398800</td>\n",
              "      <td>0.380910</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.399596</td>\n",
              "      <td>4</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>901</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.994012</td>\n",
              "      <td>0.665442</td>\n",
              "      <td>0.361626</td>\n",
              "      <td>0.612994</td>\n",
              "      <td>0.411066</td>\n",
              "      <td>0.395389</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.387487</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>902</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.996008</td>\n",
              "      <td>0.343731</td>\n",
              "      <td>0.371189</td>\n",
              "      <td>0.528542</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.408255</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.375378</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>903</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.998004</td>\n",
              "      <td>0.980854</td>\n",
              "      <td>0.325762</td>\n",
              "      <td>0.570746</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.402142</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.363269</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>904</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.536747</td>\n",
              "      <td>0.355051</td>\n",
              "      <td>0.590101</td>\n",
              "      <td>0.496633</td>\n",
              "      <td>0.401388</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.351160</td>\n",
              "      <td>4</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>4137 rows × 11 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "     Formation     Depth        GR  ...    RELPOS  Facies  Well Name\n",
              "0     0.076923  0.399818  0.298966  ...  1.000000       3          7\n",
              "1     0.076923  0.400729  0.302738  ...  0.978788       3          7\n",
              "2     0.076923  0.401639  0.306417  ...  0.956566       3          7\n",
              "3     0.076923  0.402550  0.339247  ...  0.935354       3          7\n",
              "4     0.076923  0.403461  0.285601  ...  0.914141       3          7\n",
              "..         ...       ...       ...  ...       ...     ...        ...\n",
              "900   0.923077  0.992016  0.555516  ...  0.399596       4          9\n",
              "901   0.923077  0.994012  0.665442  ...  0.387487       6          9\n",
              "902   0.923077  0.996008  0.343731  ...  0.375378       6          9\n",
              "903   0.923077  0.998004  0.980854  ...  0.363269       6          9\n",
              "904   0.923077  1.000000  0.536747  ...  0.351160       4          9\n",
              "\n",
              "[4137 rows x 11 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WQsub4cqORYf"
      },
      "source": [
        "training_data = training_data.reset_index()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        },
        "id": "-IljSAC0OdkA",
        "outputId": "ce4b162a-331b-4cb6-85a6-b62166f36825"
      },
      "source": [
        "training_data"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>index</th>\n",
              "      <th>Formation</th>\n",
              "      <th>Depth</th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "      <th>Facies</th>\n",
              "      <th>Well Name</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.399818</td>\n",
              "      <td>0.298966</td>\n",
              "      <td>0.458149</td>\n",
              "      <td>0.776042</td>\n",
              "      <td>0.219321</td>\n",
              "      <td>0.557385</td>\n",
              "      <td>0.0</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.400729</td>\n",
              "      <td>0.302738</td>\n",
              "      <td>0.456157</td>\n",
              "      <td>0.888021</td>\n",
              "      <td>0.231865</td>\n",
              "      <td>0.494046</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.978788</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>2</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.401639</td>\n",
              "      <td>0.306417</td>\n",
              "      <td>0.454165</td>\n",
              "      <td>0.903646</td>\n",
              "      <td>0.241224</td>\n",
              "      <td>0.430707</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.956566</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>3</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.402550</td>\n",
              "      <td>0.339247</td>\n",
              "      <td>0.452173</td>\n",
              "      <td>0.880208</td>\n",
              "      <td>0.242479</td>\n",
              "      <td>0.418039</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.935354</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>4</td>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.403461</td>\n",
              "      <td>0.285601</td>\n",
              "      <td>0.446860</td>\n",
              "      <td>0.869792</td>\n",
              "      <td>0.246049</td>\n",
              "      <td>0.405371</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.914141</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4132</th>\n",
              "      <td>900</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.992016</td>\n",
              "      <td>0.555516</td>\n",
              "      <td>0.335326</td>\n",
              "      <td>0.645820</td>\n",
              "      <td>0.398800</td>\n",
              "      <td>0.380910</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.399596</td>\n",
              "      <td>4</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4133</th>\n",
              "      <td>901</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.994012</td>\n",
              "      <td>0.665442</td>\n",
              "      <td>0.361626</td>\n",
              "      <td>0.612994</td>\n",
              "      <td>0.411066</td>\n",
              "      <td>0.395389</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.387487</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4134</th>\n",
              "      <td>902</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.996008</td>\n",
              "      <td>0.343731</td>\n",
              "      <td>0.371189</td>\n",
              "      <td>0.528542</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.408255</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.375378</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4135</th>\n",
              "      <td>903</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.998004</td>\n",
              "      <td>0.980854</td>\n",
              "      <td>0.325762</td>\n",
              "      <td>0.570746</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.402142</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.363269</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4136</th>\n",
              "      <td>904</td>\n",
              "      <td>0.923077</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.536747</td>\n",
              "      <td>0.355051</td>\n",
              "      <td>0.590101</td>\n",
              "      <td>0.496633</td>\n",
              "      <td>0.401388</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.351160</td>\n",
              "      <td>4</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>4137 rows × 12 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "      index  Formation     Depth        GR  ...  NM_M    RELPOS  Facies  Well Name\n",
              "0         0   0.076923  0.399818  0.298966  ...   0.0  1.000000       3          7\n",
              "1         1   0.076923  0.400729  0.302738  ...   0.0  0.978788       3          7\n",
              "2         2   0.076923  0.401639  0.306417  ...   0.0  0.956566       3          7\n",
              "3         3   0.076923  0.402550  0.339247  ...   0.0  0.935354       3          7\n",
              "4         4   0.076923  0.403461  0.285601  ...   0.0  0.914141       3          7\n",
              "...     ...        ...       ...       ...  ...   ...       ...     ...        ...\n",
              "4132    900   0.923077  0.992016  0.555516  ...   1.0  0.399596       4          9\n",
              "4133    901   0.923077  0.994012  0.665442  ...   1.0  0.387487       6          9\n",
              "4134    902   0.923077  0.996008  0.343731  ...   1.0  0.375378       6          9\n",
              "4135    903   0.923077  0.998004  0.980854  ...   1.0  0.363269       6          9\n",
              "4136    904   0.923077  1.000000  0.536747  ...   1.0  0.351160       4          9\n",
              "\n",
              "[4137 rows x 12 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VDSLyRg0Ofbq"
      },
      "source": [
        "training_data = training_data.drop(columns='index')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aSJIiIjelIF8"
      },
      "source": [
        "train_data = training_data"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "background_save": true,
          "base_uri": "https://localhost:8080/",
          "height": 419
        },
        "id": "JHUxUm48mxpN",
        "outputId": "9a216683-5aef-4626-a664-38328a04f462"
      },
      "source": [
        "training_data"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Formation</th>\n",
              "      <th>Depth</th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "      <th>Facies</th>\n",
              "      <th>Well Name</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.399818</td>\n",
              "      <td>0.298966</td>\n",
              "      <td>0.458149</td>\n",
              "      <td>0.776042</td>\n",
              "      <td>0.219321</td>\n",
              "      <td>0.557385</td>\n",
              "      <td>0.0</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.400729</td>\n",
              "      <td>0.302738</td>\n",
              "      <td>0.456157</td>\n",
              "      <td>0.888021</td>\n",
              "      <td>0.231865</td>\n",
              "      <td>0.494046</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.978788</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.401639</td>\n",
              "      <td>0.306417</td>\n",
              "      <td>0.454165</td>\n",
              "      <td>0.903646</td>\n",
              "      <td>0.241224</td>\n",
              "      <td>0.430707</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.956566</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.402550</td>\n",
              "      <td>0.339247</td>\n",
              "      <td>0.452173</td>\n",
              "      <td>0.880208</td>\n",
              "      <td>0.242479</td>\n",
              "      <td>0.418039</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.935354</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.403461</td>\n",
              "      <td>0.285601</td>\n",
              "      <td>0.446860</td>\n",
              "      <td>0.869792</td>\n",
              "      <td>0.246049</td>\n",
              "      <td>0.405371</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.914141</td>\n",
              "      <td>3</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4132</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.992016</td>\n",
              "      <td>0.555516</td>\n",
              "      <td>0.335326</td>\n",
              "      <td>0.645820</td>\n",
              "      <td>0.398800</td>\n",
              "      <td>0.380910</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.399596</td>\n",
              "      <td>4</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4133</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.994012</td>\n",
              "      <td>0.665442</td>\n",
              "      <td>0.361626</td>\n",
              "      <td>0.612994</td>\n",
              "      <td>0.411066</td>\n",
              "      <td>0.395389</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.387487</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4134</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.996008</td>\n",
              "      <td>0.343731</td>\n",
              "      <td>0.371189</td>\n",
              "      <td>0.528542</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.408255</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.375378</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4135</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.998004</td>\n",
              "      <td>0.980854</td>\n",
              "      <td>0.325762</td>\n",
              "      <td>0.570746</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.402142</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.363269</td>\n",
              "      <td>6</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4136</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.536747</td>\n",
              "      <td>0.355051</td>\n",
              "      <td>0.590101</td>\n",
              "      <td>0.496633</td>\n",
              "      <td>0.401388</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.351160</td>\n",
              "      <td>4</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>4137 rows × 11 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "      Formation     Depth        GR  ...    RELPOS  Facies  Well Name\n",
              "0      0.076923  0.399818  0.298966  ...  1.000000       3          7\n",
              "1      0.076923  0.400729  0.302738  ...  0.978788       3          7\n",
              "2      0.076923  0.401639  0.306417  ...  0.956566       3          7\n",
              "3      0.076923  0.402550  0.339247  ...  0.935354       3          7\n",
              "4      0.076923  0.403461  0.285601  ...  0.914141       3          7\n",
              "...         ...       ...       ...  ...       ...     ...        ...\n",
              "4132   0.923077  0.992016  0.555516  ...  0.399596       4          9\n",
              "4133   0.923077  0.994012  0.665442  ...  0.387487       6          9\n",
              "4134   0.923077  0.996008  0.343731  ...  0.375378       6          9\n",
              "4135   0.923077  0.998004  0.980854  ...  0.363269       6          9\n",
              "4136   0.923077  1.000000  0.536747  ...  0.351160       4          9\n",
              "\n",
              "[4137 rows x 11 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 0
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "background_save": true
        },
        "id": "4KQnNwVYR6hw",
        "scrolled": true
      },
      "source": [
        "blind_data = blind_data.drop(columns=['Unnamed: 0','Well Name'])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "background_save": true
        },
        "id": "sjN58QCagULR"
      },
      "source": [
        "train_data = train_data.drop(columns=['Well Name'])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        },
        "id": "BwJDfT2S4vHn",
        "outputId": "be3b5c0e-ce1b-4130-f99c-f9a99accdfff"
      },
      "source": [
        "train_data"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Formation</th>\n",
              "      <th>Depth</th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "      <th>Facies</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.399818</td>\n",
              "      <td>0.298966</td>\n",
              "      <td>0.458149</td>\n",
              "      <td>0.776042</td>\n",
              "      <td>0.219321</td>\n",
              "      <td>0.557385</td>\n",
              "      <td>0.0</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.400729</td>\n",
              "      <td>0.302738</td>\n",
              "      <td>0.456157</td>\n",
              "      <td>0.888021</td>\n",
              "      <td>0.231865</td>\n",
              "      <td>0.494046</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.978788</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.401639</td>\n",
              "      <td>0.306417</td>\n",
              "      <td>0.454165</td>\n",
              "      <td>0.903646</td>\n",
              "      <td>0.241224</td>\n",
              "      <td>0.430707</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.956566</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.402550</td>\n",
              "      <td>0.339247</td>\n",
              "      <td>0.452173</td>\n",
              "      <td>0.880208</td>\n",
              "      <td>0.242479</td>\n",
              "      <td>0.418039</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.935354</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.403461</td>\n",
              "      <td>0.285601</td>\n",
              "      <td>0.446860</td>\n",
              "      <td>0.869792</td>\n",
              "      <td>0.246049</td>\n",
              "      <td>0.405371</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.914141</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4132</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.992016</td>\n",
              "      <td>0.555516</td>\n",
              "      <td>0.335326</td>\n",
              "      <td>0.645820</td>\n",
              "      <td>0.398800</td>\n",
              "      <td>0.380910</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.399596</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4133</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.994012</td>\n",
              "      <td>0.665442</td>\n",
              "      <td>0.361626</td>\n",
              "      <td>0.612994</td>\n",
              "      <td>0.411066</td>\n",
              "      <td>0.395389</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.387487</td>\n",
              "      <td>6</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4134</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.996008</td>\n",
              "      <td>0.343731</td>\n",
              "      <td>0.371189</td>\n",
              "      <td>0.528542</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.408255</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.375378</td>\n",
              "      <td>6</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4135</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>0.998004</td>\n",
              "      <td>0.980854</td>\n",
              "      <td>0.325762</td>\n",
              "      <td>0.570746</td>\n",
              "      <td>0.474867</td>\n",
              "      <td>0.402142</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.363269</td>\n",
              "      <td>6</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4136</th>\n",
              "      <td>0.923077</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.536747</td>\n",
              "      <td>0.355051</td>\n",
              "      <td>0.590101</td>\n",
              "      <td>0.496633</td>\n",
              "      <td>0.401388</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.351160</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>4137 rows × 10 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "      Formation     Depth        GR  ...  NM_M    RELPOS  Facies\n",
              "0      0.076923  0.399818  0.298966  ...   0.0  1.000000       3\n",
              "1      0.076923  0.400729  0.302738  ...   0.0  0.978788       3\n",
              "2      0.076923  0.401639  0.306417  ...   0.0  0.956566       3\n",
              "3      0.076923  0.402550  0.339247  ...   0.0  0.935354       3\n",
              "4      0.076923  0.403461  0.285601  ...   0.0  0.914141       3\n",
              "...         ...       ...       ...  ...   ...       ...     ...\n",
              "4132   0.923077  0.992016  0.555516  ...   1.0  0.399596       4\n",
              "4133   0.923077  0.994012  0.665442  ...   1.0  0.387487       6\n",
              "4134   0.923077  0.996008  0.343731  ...   1.0  0.375378       6\n",
              "4135   0.923077  0.998004  0.980854  ...   1.0  0.363269       6\n",
              "4136   0.923077  1.000000  0.536747  ...   1.0  0.351160       4\n",
              "\n",
              "[4137 rows x 10 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZRPDADRfSbj_"
      },
      "source": [
        "train_x = train_data.drop(columns='Facies')"
      ],
      "execution_count": 22,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-cidJQhQSi1U"
      },
      "source": [
        "train_y = train_data['Facies'].values"
      ],
      "execution_count": 23,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1vQgrCF2SnMu"
      },
      "source": [
        "blind_x = blind_data.drop(columns='Facies')\r\n",
        "blind_y = blind_data['Facies'].values"
      ],
      "execution_count": 24,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JJg1nFAb9QbD"
      },
      "source": [
        "# SVM"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0lZm4uuJSw8S"
      },
      "source": [
        "from sklearn.metrics import accuracy_score\r\n",
        "from sklearn import metrics"
      ],
      "execution_count": 47,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K6-cIBnCS6qc"
      },
      "source": [
        "from sklearn import svm"
      ],
      "execution_count": 48,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bEsh2Y8YS7P8"
      },
      "source": [
        "clf = svm.SVC()"
      ],
      "execution_count": 49,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CoIXQWTrS-Zn"
      },
      "source": [
        "import sklearn\r\n",
        "from sklearn.metrics import f1_score\r\n",
        "from sklearn.metrics import precision_score\r\n",
        "from sklearn.metrics import recall_score"
      ],
      "execution_count": 50,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6ZDVB6G_9Jx0",
        "outputId": "0045fc8b-ace2-4828-fe50-c1ad97ac5408"
      },
      "source": [
        "clf.fit(train_x, train_y)\r\n",
        "y_pred= clf.predict(blind_x)\r\n",
        "print(\"Accuracy : %.4f\" % sklearn.metrics.accuracy_score(blind_y, y_pred))\r\n",
        "print(f'混淆矩阵')\r\n",
        "print('confusion matrix')\r\n",
        "print(metrics.confusion_matrix(blind_y, y_pred))\r\n",
        "print('classification report')\r\n",
        "print(metrics.classification_report(blind_y, y_pred))\r\n",
        "print(precision_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(recall_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(f1_score(blind_y, y_pred,average='weighted'))"
      ],
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Accuracy : 0.5277\n",
            "混淆矩阵\n",
            "confusion matrix\n",
            "[[  0  20   0   0   0   0   0   0   0]\n",
            " [  0 118  37   0   0   0   0   4   0]\n",
            " [  0  35  52  23   0   1  26  16   1]\n",
            " [  0   0   0  29   1  35   0   0   0]\n",
            " [  0   0   2   2   0  37   0   8   0]\n",
            " [  0   0   0  15   2  58   0  49   3]\n",
            " [  0   0   0   2   1   6  11   6   0]\n",
            " [  0   0   4   4   0  25   0 170   9]\n",
            " [  0   0   0   0   0   0   0  18   0]]\n",
            "classification report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           1       0.00      0.00      0.00        20\n",
            "           2       0.68      0.74      0.71       159\n",
            "           3       0.55      0.34      0.42       154\n",
            "           4       0.39      0.45      0.41        65\n",
            "           5       0.00      0.00      0.00        49\n",
            "           6       0.36      0.46      0.40       127\n",
            "           7       0.30      0.42      0.35        26\n",
            "           8       0.63      0.80      0.70       212\n",
            "           9       0.00      0.00      0.00        18\n",
            "\n",
            "    accuracy                           0.53       830\n",
            "   macro avg       0.32      0.36      0.33       830\n",
            "weighted avg       0.49      0.53      0.50       830\n",
            "\n",
            "0.48682738873368614\n",
            "0.5277108433734939\n",
            "0.4982687361832525\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
            "  _warn_prf(average, modifier, msg_start, len(result))\n",
            "/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.\n",
            "  _warn_prf(average, modifier, msg_start, len(result))\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hpXz6zpeErOA"
      },
      "source": [
        "y_pred = pd.DataFrame(y_pred)\r\n",
        "y_pred.to_csv('svm.csv')"
      ],
      "execution_count": 52,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VtGjuH6A9TJ6"
      },
      "source": [
        "# XGBoost"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZSETCZpB9LC8",
        "outputId": "1f8f8792-3ba8-43ab-9904-e8cbfc7a5267"
      },
      "source": [
        "from sklearn import metrics\r\n",
        "import xgboost\r\n",
        "import numpy as np\r\n",
        "# 构建XGBoost分类器\r\n",
        "xgboost = xgboost.XGBClassifier()\r\n",
        "# 使用重抽样后的数据，对其建模\r\n",
        "xgboost.fit(train_x,train_y)\r\n",
        "# 将模型运用到测试数据集中\r\n",
        "y_pred = xgboost.predict(blind_x) #传入的是array\r\n",
        "print(\"Accuracy : %.4f\" % sklearn.metrics.accuracy_score(blind_y, y_pred))\r\n",
        "print(f'混淆矩阵')\r\n",
        "print('confusion matrix')\r\n",
        "print(metrics.confusion_matrix(blind_y, y_pred))\r\n",
        "print('classification report')\r\n",
        "print(metrics.classification_report(blind_y, y_pred))\r\n",
        "print(precision_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(recall_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(f1_score(blind_y, y_pred,average='weighted'))"
      ],
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Accuracy : 0.4964\n",
            "混淆矩阵\n",
            "confusion matrix\n",
            "[[  2  10   8   0   0   0   0   0   0]\n",
            " [ 44  49  62   0   0   1   0   3   0]\n",
            " [  0  17  76  15   0   4  34   8   0]\n",
            " [  0   0   3  37  11  13   0   1   0]\n",
            " [  0   0   3   2   8  24   0  12   0]\n",
            " [  0   0  16   6   3  64   0  37   1]\n",
            " [  0   0   0   2   5   1  11   7   0]\n",
            " [  0   1   5   4   0  32   4 165   1]\n",
            " [  0   0   0   0   0   0   0  18   0]]\n",
            "classification report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           1       0.04      0.10      0.06        20\n",
            "           2       0.64      0.31      0.42       159\n",
            "           3       0.44      0.49      0.46       154\n",
            "           4       0.56      0.57      0.56        65\n",
            "           5       0.30      0.16      0.21        49\n",
            "           6       0.46      0.50      0.48       127\n",
            "           7       0.22      0.42      0.29        26\n",
            "           8       0.66      0.78      0.71       212\n",
            "           9       0.00      0.00      0.00        18\n",
            "\n",
            "    accuracy                           0.50       830\n",
            "   macro avg       0.37      0.37      0.36       830\n",
            "weighted avg       0.51      0.50      0.49       830\n",
            "\n",
            "0.511248891188473\n",
            "0.4963855421686747\n",
            "0.4887903012366966\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Rdbv2pUtE3bf"
      },
      "source": [
        "y_pred = pd.DataFrame(y_pred)\r\n",
        "y_pred.to_csv('XGBoost.csv')"
      ],
      "execution_count": 55,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rse6gGyH9aDT"
      },
      "source": [
        "# GBDT"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bAhrG9GP9VYC",
        "outputId": "c3f3edc4-a2a3-4ece-996c-09cfc55512b4"
      },
      "source": [
        "max_depth=40\r\n",
        "max_features=100\r\n",
        "max_features=100\r\n",
        "from sklearn.ensemble import GradientBoostingClassifier\r\n",
        "import sklearn.metrics\r\n",
        "gbm0 = GradientBoostingClassifier(n_estimators=1000, learning_rate=0.01, random_state=42).fit(train_x, train_y)\r\n",
        "y_pred= gbm0.predict(blind_x)\r\n",
        "print(\"Accuracy : %.4f\" % sklearn.metrics.accuracy_score(blind_y, y_pred))\r\n",
        "print(f'混淆矩阵')\r\n",
        "print('confusion matrix')\r\n",
        "print(metrics.confusion_matrix(blind_y, y_pred))\r\n",
        "print('classification report')\r\n",
        "print(metrics.classification_report(blind_y, y_pred))\r\n",
        "print(precision_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(recall_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(f1_score(blind_y, y_pred,average='weighted'))"
      ],
      "execution_count": 57,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Accuracy : 0.4361\n",
            "混淆矩阵\n",
            "confusion matrix\n",
            "[[  4   9   7   0   0   0   0   0   0]\n",
            " [ 40  36  79   0   0   0   0   4   0]\n",
            " [  1  13  87   9   0   1  34   6   3]\n",
            " [  0   0   9  17  22  11   0   6   0]\n",
            " [  0   0   4   2   1  29   1  11   1]\n",
            " [  0   0  14   5   2  60   0  39   7]\n",
            " [  0   0   2   1   5   7   3   7   1]\n",
            " [  0   1   5   4   1  26   9 152  14]\n",
            " [  0   0   0   0   0   0   0  16   2]]\n",
            "classification report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           1       0.09      0.20      0.12        20\n",
            "           2       0.61      0.23      0.33       159\n",
            "           3       0.42      0.56      0.48       154\n",
            "           4       0.45      0.26      0.33        65\n",
            "           5       0.03      0.02      0.03        49\n",
            "           6       0.45      0.47      0.46       127\n",
            "           7       0.06      0.12      0.08        26\n",
            "           8       0.63      0.72      0.67       212\n",
            "           9       0.07      0.11      0.09        18\n",
            "\n",
            "    accuracy                           0.44       830\n",
            "   macro avg       0.31      0.30      0.29       830\n",
            "weighted avg       0.47      0.44      0.43       830\n",
            "\n",
            "0.46710778446415196\n",
            "0.43614457831325304\n",
            "0.42921214624952897\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9wA8kzscE-zB"
      },
      "source": [
        "y_pred = pd.DataFrame(y_pred)\r\n",
        "y_pred.to_csv('GBDT.csv')"
      ],
      "execution_count": 58,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "THcuz1Qb91Mt"
      },
      "source": [
        "# 随机森林"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qfYeCOHA9bjb",
        "outputId": "02cb25d1-cb97-4c7e-dbbd-32df8242c4f2"
      },
      "source": [
        "from sklearn.ensemble import RandomForestClassifier\r\n",
        "from sklearn import metrics\r\n",
        "\r\n",
        "rfmodel = RandomForestClassifier()\r\n",
        "rfmodel.fit(train_x,train_y)\r\n",
        "print('model')\r\n",
        "print(rfmodel) \r\n",
        "\r\n",
        "ypredrf1 = rfmodel.predict(blind_x)\r\n",
        "print('confusion matrix')\r\n",
        "print(metrics.confusion_matrix(blind_y, ypredrf1))\r\n",
        "print('classification report')\r\n",
        "print(metrics.classification_report(blind_y, ypredrf1))\r\n",
        "print('Accuracy : %f' % (metrics.accuracy_score(blind_y, ypredrf1)))\r\n",
        "print(precision_score(blind_y, ypredrf1, average='weighted'))\r\n",
        "print(recall_score(blind_y, ypredrf1, average='weighted'))\r\n",
        "print(f1_score(blind_y, ypredrf1,average='weighted'))"
      ],
      "execution_count": 59,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "model\n",
            "RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,\n",
            "                       criterion='gini', max_depth=None, max_features='auto',\n",
            "                       max_leaf_nodes=None, max_samples=None,\n",
            "                       min_impurity_decrease=0.0, min_impurity_split=None,\n",
            "                       min_samples_leaf=1, min_samples_split=2,\n",
            "                       min_weight_fraction_leaf=0.0, n_estimators=100,\n",
            "                       n_jobs=None, oob_score=False, random_state=None,\n",
            "                       verbose=0, warm_start=False)\n",
            "confusion matrix\n",
            "[[  0  15   5   0   0   0   0   0   0]\n",
            " [ 10  68  76   0   1   2   0   2   0]\n",
            " [  0  20  69  11   3  13  24  14   0]\n",
            " [  0   0   0  19  21  11   0  14   0]\n",
            " [  0   1   2   0   0  17   0  29   0]\n",
            " [  0   0  13   5   2  49   0  58   0]\n",
            " [  0   0   0   1   3   4   2  16   0]\n",
            " [  0   2   2   2   6  24   0 169   7]\n",
            " [  0   0   0   0   0   0   0  18   0]]\n",
            "classification report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           1       0.00      0.00      0.00        20\n",
            "           2       0.64      0.43      0.51       159\n",
            "           3       0.41      0.45      0.43       154\n",
            "           4       0.50      0.29      0.37        65\n",
            "           5       0.00      0.00      0.00        49\n",
            "           6       0.41      0.39      0.40       127\n",
            "           7       0.08      0.08      0.08        26\n",
            "           8       0.53      0.80      0.64       212\n",
            "           9       0.00      0.00      0.00        18\n",
            "\n",
            "    accuracy                           0.45       830\n",
            "   macro avg       0.29      0.27      0.27       830\n",
            "weighted avg       0.44      0.45      0.43       830\n",
            "\n",
            "Accuracy : 0.453012\n",
            "0.4384934648774739\n",
            "0.4530120481927711\n",
            "0.43236937746003246\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zC__OMhxFCtW"
      },
      "source": [
        "y_pred = pd.DataFrame(y_pred)\r\n",
        "y_pred.to_csv('随机森林.csv')"
      ],
      "execution_count": 60,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WCGxxjWQ-DZP"
      },
      "source": [
        "# SMOTE 系列方法"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "EvljrjYy-mI-"
      },
      "source": [
        "## SMOTE过采样"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OwYRe5Xa95Qb"
      },
      "source": [
        "a = training_data['Facies'].value_counts()"
      ],
      "execution_count": 61,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_NTlrQoz-6Wr",
        "outputId": "dbff0b79-53ef-4960-c452-dc0ad17004b1"
      },
      "source": [
        "a"
      ],
      "execution_count": 62,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2    940\n",
              "3    780\n",
              "8    686\n",
              "6    582\n",
              "5    296\n",
              "4    271\n",
              "1    268\n",
              "9    173\n",
              "7    141\n",
              "Name: Facies, dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 62
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uyi9o2vD-68Z",
        "outputId": "5a091396-447f-474c-bb49-d061f235cda3"
      },
      "source": [
        "from imblearn.over_sampling import SMOTE\r\n",
        "from collections import Counter\r\n",
        "sm = SMOTE(sampling_strategy={5:500, 4:500, 1:500, 9:500, 7:500},random_state=24)\r\n",
        "train_x_sm, train_y_sm = sm.fit_sample(train_x, train_y)\r\n",
        "train_x_sm = pd.DataFrame(train_x_sm)\r\n",
        "train_y_sm = pd.DataFrame(train_y_sm)"
      ],
      "execution_count": 63,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function safe_indexing is deprecated; safe_indexing is deprecated in version 0.22 and will be removed in version 0.24.\n",
            "  warnings.warn(msg, category=FutureWarning)\n",
            "/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function safe_indexing is deprecated; safe_indexing is deprecated in version 0.22 and will be removed in version 0.24.\n",
            "  warnings.warn(msg, category=FutureWarning)\n",
            "/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function safe_indexing is deprecated; safe_indexing is deprecated in version 0.22 and will be removed in version 0.24.\n",
            "  warnings.warn(msg, category=FutureWarning)\n",
            "/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function safe_indexing is deprecated; safe_indexing is deprecated in version 0.22 and will be removed in version 0.24.\n",
            "  warnings.warn(msg, category=FutureWarning)\n",
            "/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function safe_indexing is deprecated; safe_indexing is deprecated in version 0.22 and will be removed in version 0.24.\n",
            "  warnings.warn(msg, category=FutureWarning)\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        },
        "id": "-m2ujOmcBK7n",
        "outputId": "67bb0c0a-ff0c-44ae-9c9e-b19eabc36952"
      },
      "source": [
        "train_x_sm"
      ],
      "execution_count": 64,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>0</th>\n",
              "      <th>1</th>\n",
              "      <th>2</th>\n",
              "      <th>3</th>\n",
              "      <th>4</th>\n",
              "      <th>5</th>\n",
              "      <th>6</th>\n",
              "      <th>7</th>\n",
              "      <th>8</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.399818</td>\n",
              "      <td>0.298966</td>\n",
              "      <td>0.458149</td>\n",
              "      <td>0.776042</td>\n",
              "      <td>0.219321</td>\n",
              "      <td>0.557385</td>\n",
              "      <td>0.0</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.400729</td>\n",
              "      <td>0.302738</td>\n",
              "      <td>0.456157</td>\n",
              "      <td>0.888021</td>\n",
              "      <td>0.231865</td>\n",
              "      <td>0.494046</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.978788</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.401639</td>\n",
              "      <td>0.306417</td>\n",
              "      <td>0.454165</td>\n",
              "      <td>0.903646</td>\n",
              "      <td>0.241224</td>\n",
              "      <td>0.430707</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.956566</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.402550</td>\n",
              "      <td>0.339247</td>\n",
              "      <td>0.452173</td>\n",
              "      <td>0.880208</td>\n",
              "      <td>0.242479</td>\n",
              "      <td>0.418039</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.935354</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.403461</td>\n",
              "      <td>0.285601</td>\n",
              "      <td>0.446860</td>\n",
              "      <td>0.869792</td>\n",
              "      <td>0.246049</td>\n",
              "      <td>0.405371</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.914141</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5483</th>\n",
              "      <td>0.769231</td>\n",
              "      <td>0.896945</td>\n",
              "      <td>0.131065</td>\n",
              "      <td>0.174967</td>\n",
              "      <td>0.566944</td>\n",
              "      <td>0.326708</td>\n",
              "      <td>0.706382</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.598604</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5484</th>\n",
              "      <td>0.769231</td>\n",
              "      <td>0.899811</td>\n",
              "      <td>0.118115</td>\n",
              "      <td>0.130846</td>\n",
              "      <td>0.585812</td>\n",
              "      <td>0.295335</td>\n",
              "      <td>0.836811</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.545584</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5485</th>\n",
              "      <td>0.769231</td>\n",
              "      <td>0.904665</td>\n",
              "      <td>0.114625</td>\n",
              "      <td>0.087619</td>\n",
              "      <td>0.570939</td>\n",
              "      <td>0.357633</td>\n",
              "      <td>0.665547</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.456075</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5486</th>\n",
              "      <td>0.307692</td>\n",
              "      <td>0.800601</td>\n",
              "      <td>0.088778</td>\n",
              "      <td>0.560680</td>\n",
              "      <td>0.481497</td>\n",
              "      <td>0.176231</td>\n",
              "      <td>0.603506</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.593949</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5487</th>\n",
              "      <td>0.769231</td>\n",
              "      <td>0.921084</td>\n",
              "      <td>0.251281</td>\n",
              "      <td>0.573498</td>\n",
              "      <td>0.619204</td>\n",
              "      <td>0.136666</td>\n",
              "      <td>0.454470</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.152568</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5488 rows × 9 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "             0         1         2         3  ...         5         6    7         8\n",
              "0     0.076923  0.399818  0.298966  0.458149  ...  0.219321  0.557385  0.0  1.000000\n",
              "1     0.076923  0.400729  0.302738  0.456157  ...  0.231865  0.494046  0.0  0.978788\n",
              "2     0.076923  0.401639  0.306417  0.454165  ...  0.241224  0.430707  0.0  0.956566\n",
              "3     0.076923  0.402550  0.339247  0.452173  ...  0.242479  0.418039  0.0  0.935354\n",
              "4     0.076923  0.403461  0.285601  0.446860  ...  0.246049  0.405371  0.0  0.914141\n",
              "...        ...       ...       ...       ...  ...       ...       ...  ...       ...\n",
              "5483  0.769231  0.896945  0.131065  0.174967  ...  0.326708  0.706382  1.0  0.598604\n",
              "5484  0.769231  0.899811  0.118115  0.130846  ...  0.295335  0.836811  1.0  0.545584\n",
              "5485  0.769231  0.904665  0.114625  0.087619  ...  0.357633  0.665547  1.0  0.456075\n",
              "5486  0.307692  0.800601  0.088778  0.560680  ...  0.176231  0.603506  1.0  0.593949\n",
              "5487  0.769231  0.921084  0.251281  0.573498  ...  0.136666  0.454470  1.0  0.152568\n",
              "\n",
              "[5488 rows x 9 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 64
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5YVaWzBzBM4t"
      },
      "source": [
        "train_x_sm.columns = ['Formation', 'Depth', 'GR', 'ILD_log10', 'DeltaPHI', 'PHIND', 'PE', 'NM_M', 'RELPOS']"
      ],
      "execution_count": 65,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        },
        "id": "u9O9r3sSB7Ty",
        "outputId": "e20eaf9f-d9d8-45bc-e02e-5f0595e1a37d"
      },
      "source": [
        "train_x_sm"
      ],
      "execution_count": 66,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Formation</th>\n",
              "      <th>Depth</th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.399818</td>\n",
              "      <td>0.298966</td>\n",
              "      <td>0.458149</td>\n",
              "      <td>0.776042</td>\n",
              "      <td>0.219321</td>\n",
              "      <td>0.557385</td>\n",
              "      <td>0.0</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.400729</td>\n",
              "      <td>0.302738</td>\n",
              "      <td>0.456157</td>\n",
              "      <td>0.888021</td>\n",
              "      <td>0.231865</td>\n",
              "      <td>0.494046</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.978788</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.401639</td>\n",
              "      <td>0.306417</td>\n",
              "      <td>0.454165</td>\n",
              "      <td>0.903646</td>\n",
              "      <td>0.241224</td>\n",
              "      <td>0.430707</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.956566</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.402550</td>\n",
              "      <td>0.339247</td>\n",
              "      <td>0.452173</td>\n",
              "      <td>0.880208</td>\n",
              "      <td>0.242479</td>\n",
              "      <td>0.418039</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.935354</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0.076923</td>\n",
              "      <td>0.403461</td>\n",
              "      <td>0.285601</td>\n",
              "      <td>0.446860</td>\n",
              "      <td>0.869792</td>\n",
              "      <td>0.246049</td>\n",
              "      <td>0.405371</td>\n",
              "      <td>0.0</td>\n",
              "      <td>0.914141</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5483</th>\n",
              "      <td>0.769231</td>\n",
              "      <td>0.896945</td>\n",
              "      <td>0.131065</td>\n",
              "      <td>0.174967</td>\n",
              "      <td>0.566944</td>\n",
              "      <td>0.326708</td>\n",
              "      <td>0.706382</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.598604</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5484</th>\n",
              "      <td>0.769231</td>\n",
              "      <td>0.899811</td>\n",
              "      <td>0.118115</td>\n",
              "      <td>0.130846</td>\n",
              "      <td>0.585812</td>\n",
              "      <td>0.295335</td>\n",
              "      <td>0.836811</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.545584</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5485</th>\n",
              "      <td>0.769231</td>\n",
              "      <td>0.904665</td>\n",
              "      <td>0.114625</td>\n",
              "      <td>0.087619</td>\n",
              "      <td>0.570939</td>\n",
              "      <td>0.357633</td>\n",
              "      <td>0.665547</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.456075</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5486</th>\n",
              "      <td>0.307692</td>\n",
              "      <td>0.800601</td>\n",
              "      <td>0.088778</td>\n",
              "      <td>0.560680</td>\n",
              "      <td>0.481497</td>\n",
              "      <td>0.176231</td>\n",
              "      <td>0.603506</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.593949</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5487</th>\n",
              "      <td>0.769231</td>\n",
              "      <td>0.921084</td>\n",
              "      <td>0.251281</td>\n",
              "      <td>0.573498</td>\n",
              "      <td>0.619204</td>\n",
              "      <td>0.136666</td>\n",
              "      <td>0.454470</td>\n",
              "      <td>1.0</td>\n",
              "      <td>0.152568</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5488 rows × 9 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "      Formation     Depth        GR  ...        PE  NM_M    RELPOS\n",
              "0      0.076923  0.399818  0.298966  ...  0.557385   0.0  1.000000\n",
              "1      0.076923  0.400729  0.302738  ...  0.494046   0.0  0.978788\n",
              "2      0.076923  0.401639  0.306417  ...  0.430707   0.0  0.956566\n",
              "3      0.076923  0.402550  0.339247  ...  0.418039   0.0  0.935354\n",
              "4      0.076923  0.403461  0.285601  ...  0.405371   0.0  0.914141\n",
              "...         ...       ...       ...  ...       ...   ...       ...\n",
              "5483   0.769231  0.896945  0.131065  ...  0.706382   1.0  0.598604\n",
              "5484   0.769231  0.899811  0.118115  ...  0.836811   1.0  0.545584\n",
              "5485   0.769231  0.904665  0.114625  ...  0.665547   1.0  0.456075\n",
              "5486   0.307692  0.800601  0.088778  ...  0.603506   1.0  0.593949\n",
              "5487   0.769231  0.921084  0.251281  ...  0.454470   1.0  0.152568\n",
              "\n",
              "[5488 rows x 9 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 66
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iSExn0yv_KVv"
      },
      "source": [
        "## SMOTE SVM"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gNP7KqvO_Fkr"
      },
      "source": [
        "clf = svm.SVC()"
      ],
      "execution_count": 42,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NonoIewA_O5I",
        "outputId": "cff6e6df-cfa3-4d0d-ee79-561886057ecb"
      },
      "source": [
        "clf.fit(train_x_sm, train_y_sm)\r\n",
        "y_pred= clf.predict(blind_x)\r\n",
        "print(\"Accuracy : %.4f\" % sklearn.metrics.accuracy_score(blind_y, y_pred))\r\n",
        "print(f'混淆矩阵')\r\n",
        "print('confusion matrix')\r\n",
        "print(metrics.confusion_matrix(blind_y, y_pred))\r\n",
        "print('classification report')\r\n",
        "print(metrics.classification_report(blind_y, y_pred))\r\n",
        "print(precision_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(recall_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(f1_score(blind_y, y_pred,average='weighted'))"
      ],
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py:760: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
            "  y = column_or_1d(y, warn=True)\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Accuracy : 0.4699\n",
            "混淆矩阵\n",
            "confusion matrix\n",
            "[[  9  11   0   0   0   0   0   0   0]\n",
            " [ 36  82  37   0   0   0   0   4   0]\n",
            " [  3  32  52  21   4   1  26   6   9]\n",
            " [  0   0   0  38   5  22   0   0   0]\n",
            " [  0   0   2   2   2  36   0   6   1]\n",
            " [  0   0   0  17   4  52   1  42  11]\n",
            " [  0   0   0   3   3   3  16   1   0]\n",
            " [  0   0   4   7   2  22   4 138  35]\n",
            " [  0   0   0   0   0   0   0  17   1]]\n",
            "classification report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           1       0.19      0.45      0.26        20\n",
            "           2       0.66      0.52      0.58       159\n",
            "           3       0.55      0.34      0.42       154\n",
            "           4       0.43      0.58      0.50        65\n",
            "           5       0.10      0.04      0.06        49\n",
            "           6       0.38      0.41      0.40       127\n",
            "           7       0.34      0.62      0.44        26\n",
            "           8       0.64      0.65      0.65       212\n",
            "           9       0.02      0.06      0.03        18\n",
            "\n",
            "    accuracy                           0.47       830\n",
            "   macro avg       0.37      0.41      0.37       830\n",
            "weighted avg       0.51      0.47      0.48       830\n",
            "\n",
            "0.5057263564662675\n",
            "0.46987951807228917\n",
            "0.47712092876504064\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Q8Tuc8H4Fnf1"
      },
      "source": [
        "y_pred = pd.DataFrame(y_pred)\r\n",
        "y_pred.to_csv('SMOTE_SVM.csv')"
      ],
      "execution_count": 67,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aIX1vF1V_cFY"
      },
      "source": [
        "## SMOTE XGBoost"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "q40UgHeg_U_5",
        "outputId": "0c3cb066-f6c2-448d-d0ae-1f7855ad05f2"
      },
      "source": [
        "from sklearn import metrics\r\n",
        "import xgboost\r\n",
        "import numpy as np\r\n",
        "# 构建XGBoost分类器\r\n",
        "xgboost = xgboost.XGBClassifier()\r\n",
        "# 使用重抽样后的数据，对其建模\r\n",
        "xgboost.fit(train_x_sm,train_y_sm)\r\n",
        "# 将模型运用到测试数据集中\r\n",
        "y_pred = xgboost.predict(blind_x) #传入的是array\r\n",
        "print(\"Accuracy : %.4f\" % sklearn.metrics.accuracy_score(blind_y, y_pred))\r\n",
        "print(f'混淆矩阵')\r\n",
        "print('confusion matrix')\r\n",
        "print(metrics.confusion_matrix(blind_y, y_pred))\r\n",
        "print('classification report')\r\n",
        "print(metrics.classification_report(blind_y, y_pred))\r\n",
        "print(precision_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(recall_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(f1_score(blind_y, y_pred,average='weighted'))"
      ],
      "execution_count": 68,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/sklearn/preprocessing/_label.py:235: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
            "  y = column_or_1d(y, warn=True)\n",
            "/usr/local/lib/python3.7/dist-packages/sklearn/preprocessing/_label.py:268: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
            "  y = column_or_1d(y, warn=True)\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Accuracy : 0.4627\n",
            "混淆矩阵\n",
            "confusion matrix\n",
            "[[  6   8   6   0   0   0   0   0   0]\n",
            " [ 59  28  68   0   0   3   0   1   0]\n",
            " [  3  18  72  12   0   1  35  12   1]\n",
            " [  0   0   3  40  15   7   0   0   0]\n",
            " [  0   0   5   3  10  23   2   6   0]\n",
            " [  0   0  17   6   3  61   3  35   2]\n",
            " [  0   0   1   2   3   2  16   2   0]\n",
            " [  0   0   5   7   3  29   9 150   9]\n",
            " [  0   0   0   0   0   0   0  17   1]]\n",
            "classification report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           1       0.09      0.30      0.14        20\n",
            "           2       0.52      0.18      0.26       159\n",
            "           3       0.41      0.47      0.44       154\n",
            "           4       0.57      0.62      0.59        65\n",
            "           5       0.29      0.20      0.24        49\n",
            "           6       0.48      0.48      0.48       127\n",
            "           7       0.25      0.62      0.35        26\n",
            "           8       0.67      0.71      0.69       212\n",
            "           9       0.08      0.06      0.06        18\n",
            "\n",
            "    accuracy                           0.46       830\n",
            "   macro avg       0.37      0.40      0.36       830\n",
            "weighted avg       0.49      0.46      0.46       830\n",
            "\n",
            "0.49431021590587465\n",
            "0.46265060240963857\n",
            "0.4573553815076574\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WhHKmid-F1Wb"
      },
      "source": [
        "y_pred = pd.DataFrame(y_pred)\r\n",
        "y_pred.to_csv('SMOTE_XGBoost.csv')"
      ],
      "execution_count": 69,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fRb2_OUhCB1u"
      },
      "source": [
        "## SMOTE GBDT"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lPTgbOUo_mFC",
        "outputId": "e0a4f2bf-b4e6-4f05-beac-f2d9e6f77906"
      },
      "source": [
        "max_depth=40\r\n",
        "max_features=100\r\n",
        "max_features=100\r\n",
        "from sklearn.ensemble import GradientBoostingClassifier\r\n",
        "import sklearn.metrics\r\n",
        "gbm0 = GradientBoostingClassifier(n_estimators=1000, learning_rate=0.01, random_state=42).fit(train_x_sm, train_y_sm)\r\n",
        "y_pred= gbm0.predict(blind_x)\r\n",
        "print(\"Accuracy : %.4f\" % sklearn.metrics.accuracy_score(blind_y, y_pred))\r\n",
        "print(f'混淆矩阵')\r\n",
        "print('confusion matrix')\r\n",
        "print(metrics.confusion_matrix(blind_y, y_pred))\r\n",
        "print('classification report')\r\n",
        "print(metrics.classification_report(blind_y, y_pred))\r\n",
        "print(precision_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(recall_score(blind_y, y_pred, average='weighted'))\r\n",
        "print(f1_score(blind_y, y_pred,average='weighted'))"
      ],
      "execution_count": 70,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/sklearn/ensemble/_gb.py:1454: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
            "  y = column_or_1d(y, warn=True)\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Accuracy : 0.4578\n",
            "混淆矩阵\n",
            "confusion matrix\n",
            "[[  4   9   7   0   0   0   0   0   0]\n",
            " [ 43  33  79   0   0   0   0   4   0]\n",
            " [  1  12  95   7   0   1  34   4   0]\n",
            " [  0   0   7  22  20  15   0   1   0]\n",
            " [  0   0   4   2   4  31   0   6   2]\n",
            " [  0   0  14   4   1  60   1  39   8]\n",
            " [  0   0   4   2   3   4  10   3   0]\n",
            " [  0   0   7   4   2  31  10 150   8]\n",
            " [  0   0   0   0   0   0   0  16   2]]\n",
            "classification report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           1       0.08      0.20      0.12        20\n",
            "           2       0.61      0.21      0.31       159\n",
            "           3       0.44      0.62      0.51       154\n",
            "           4       0.54      0.34      0.42        65\n",
            "           5       0.13      0.08      0.10        49\n",
            "           6       0.42      0.47      0.45       127\n",
            "           7       0.18      0.38      0.25        26\n",
            "           8       0.67      0.71      0.69       212\n",
            "           9       0.10      0.11      0.11        18\n",
            "\n",
            "    accuracy                           0.46       830\n",
            "   macro avg       0.35      0.35      0.33       830\n",
            "weighted avg       0.49      0.46      0.45       830\n",
            "\n",
            "0.49452314663266184\n",
            "0.4578313253012048\n",
            "0.4501292654261309\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FOzGveZ6F8cK"
      },
      "source": [
        "y_pred = pd.DataFrame(y_pred)\r\n",
        "y_pred.to_csv('SMOTE_GBDT.csv')"
      ],
      "execution_count": 71,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Hg0IPHwpCmkB"
      },
      "source": [
        "## SMOTE 随机森林"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1fbdLlwLCjie",
        "outputId": "2f460435-b4c0-44a7-f651-ea9dfea1b1f4"
      },
      "source": [
        "from sklearn.ensemble import RandomForestClassifier\r\n",
        "from sklearn import metrics\r\n",
        "\r\n",
        "rfmodel = RandomForestClassifier()\r\n",
        "rfmodel.fit(train_x_sm,train_y_sm)\r\n",
        "print('model')\r\n",
        "print(rfmodel) \r\n",
        "\r\n",
        "ypredrf1 = rfmodel.predict(blind_x)\r\n",
        "print('confusion matrix')\r\n",
        "print(metrics.confusion_matrix(blind_y, ypredrf1))\r\n",
        "print('classification report')\r\n",
        "print(metrics.classification_report(blind_y, ypredrf1))\r\n",
        "print('Accuracy : %f' % (metrics.accuracy_score(blind_y, ypredrf1)))\r\n",
        "print(precision_score(blind_y, ypredrf1, average='weighted'))\r\n",
        "print(recall_score(blind_y, ypredrf1, average='weighted'))\r\n",
        "print(f1_score(blind_y, ypredrf1,average='weighted'))"
      ],
      "execution_count": 72,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples,), for example using ravel().\n",
            "  \"\"\"\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "model\n",
            "RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,\n",
            "                       criterion='gini', max_depth=None, max_features='auto',\n",
            "                       max_leaf_nodes=None, max_samples=None,\n",
            "                       min_impurity_decrease=0.0, min_impurity_split=None,\n",
            "                       min_samples_leaf=1, min_samples_split=2,\n",
            "                       min_weight_fraction_leaf=0.0, n_estimators=100,\n",
            "                       n_jobs=None, oob_score=False, random_state=None,\n",
            "                       verbose=0, warm_start=False)\n",
            "confusion matrix\n",
            "[[  0  15   5   0   0   0   0   0   0]\n",
            " [  9  91  55   0   1   0   0   3   0]\n",
            " [  0  21  71  11   1   6  25  19   0]\n",
            " [  0   0   2  30  19   8   0   6   0]\n",
            " [  0   0   2   1   3  11   0  32   0]\n",
            " [  0   0  13   4   3  46   1  59   1]\n",
            " [  0   0   0   1   4   0   2  19   0]\n",
            " [  0   4   1   2   3  16   3 173  10]\n",
            " [  0   0   0   0   0   0   0  17   1]]\n",
            "classification report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           1       0.00      0.00      0.00        20\n",
            "           2       0.69      0.57      0.63       159\n",
            "           3       0.48      0.46      0.47       154\n",
            "           4       0.61      0.46      0.53        65\n",
            "           5       0.09      0.06      0.07        49\n",
            "           6       0.53      0.36      0.43       127\n",
            "           7       0.06      0.08      0.07        26\n",
            "           8       0.53      0.82      0.64       212\n",
            "           9       0.08      0.06      0.07        18\n",
            "\n",
            "    accuracy                           0.50       830\n",
            "   macro avg       0.34      0.32      0.32       830\n",
            "weighted avg       0.49      0.50      0.49       830\n",
            "\n",
            "Accuracy : 0.502410\n",
            "0.49409192482770864\n",
            "0.5024096385542168\n",
            "0.4857472871450885\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mHL1GekxCvHF"
      },
      "source": [
        "y_pred = pd.DataFrame(y_pred)\r\n",
        "y_pred.to_csv('SMOTE_随机森林.csv')"
      ],
      "execution_count": 73,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3R2uLlJiGfSZ"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}